package css.sword.util;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
public class FileUploadUtils  {

    /**
     * 
     *@name    获取文件
     *@description 暂时把文件服务器取到文件放到临时目录
     */
    public static File getFilebyMD5path(String fileid, String filetype) throws Exception, IOException {
        String tempFile =
            System.getProperty("java.io.tmpdir") + "/download_tmp" + UUID.randomUUID().toString() + "." + filetype;
        HttpClientUtils.getInstance().httpDownloadFile(getFileServerUrl() + "file/download" +
            "?md5Path=" + fileid + getFileParam(), tempFile, null);
        final File downLoadFile = new File(tempFile);
        return downLoadFile;
    }
    public static String getFileServerUrl(){
        return "https://10.2.57.77:8083/";
    }
    public static String getOsName() {
        final String os = System.getProperty("os.name");
        return os.toUpperCase();
    }
    private static String getFileParam(){
        //测试可使用staticToken
        String res = "&staticToken=f1634ed30b374f159a684ef353184f16";
        //正式使用需要获取identityId和token
//        String res = "&identityId=" + "identityId" + "&token=" + "token";
        return res;
}
    public static void main1(String[] args) {
        try {
            getFilebyMD5path("092e6bd74afcf3cb33d077b3c16a3edd@178345","jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
         Map<String, String> uploadParams = new LinkedHashMap<>();
         File outFile = new File("D:\\121.txt");
        String md5= Md5Util.md5(new  FileInputStream(outFile));
        String fileSize = String.valueOf(outFile.length());
        uploadParams.put("userId", "currentIdentityDm");
        uploadParams.put("moduleName", "nwiiii");
        uploadParams.put("fileSrcName", "文件名称--20190220");
        uploadParams.put("fileSize", fileSize);
        uploadParams.put("md5", md5);
        uploadParams.put("staticToken", "f1634ed30b374f159a684ef353184f16");
        InputStream in = new FileInputStream(outFile);
        if(!isExistInFileServer(uploadParams)){
//                String result =HttpClientUtils.getInstance().uploadFileImpl(getFileServerUrl()+"/file/uploadDfs/",
//                        outFile.getAbsolutePath(), "file", uploadParams);
                String result =HttpClientUtils.getInstance().uploadFileImpl(getFileServerUrl()+"/file/uploadDfs/",
                    in, "file", uploadParams,".txt");
        }else{
            //uploadParams 可根据业务需求进行调整,如返回想要的结果等
            StringBuffer bf = new StringBuffer("?");
            for (Map.Entry<String, String> e : uploadParams.entrySet()) {
                bf.append(e.getKey());
                bf.append("=");
                bf.append(e.getValue());
                bf.append("&");
            }
            System.out.println(bf.toString());
        }

    }


    public static boolean isExistInFileServer( Map<String, String> uploadParams) throws FileNotFoundException {
;          StringBuffer bf = new StringBuffer("?");
        for (Map.Entry<String, String> e : uploadParams.entrySet()) {
            bf.append(e.getKey());
            bf.append("=");
            bf.append(e.getValue());
            bf.append("&");
        }

        String result =
            HttpClientUtils.getInstance().httpGet(getFileServerUrl()+"/file/md5CheckUpload"+bf.toString(),uploadParams);
        JSONObject resultObj = JSONObject.parseObject(result);
        if(null!=resultObj){
           String isExite = resultObj.getString("message");
           if(isExite.equals("0")){
               return true;
           }
        }
        return false;
    }
}
